<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>WebSocket Test</title>
</head>
<body>
    <h1>WebSocket Test</h1>
    <input type="text" id="messageInput" placeholder="Enter a message...">
    <button onclick="sendMessage()">Send</button>
    <ul id="messages"></ul>

    <script>
        const ws = new WebSocket('ws://localhost:8080');

        ws.onopen = () => {
            console.log('Connected to server');
        };

        ws.onmessage = (event) => {
            const messages = document.getElementById('messages');
            const li = document.createElement('li');
            li.textContent = `Received: ${event.data}`;
            messages.appendChild(li);
        };

        ws.onerror = (error) => {
            console.error('WebSocket error:', error);
        };

        ws.onclose = () => {
            console.log('Disconnected from server');
        };

        function sendMessage() {
            const input = document.getElementById('messageInput');
            const message = input.value;
            ws.send(message);
            input.value = '';
        }
    </script>
</body>
</html>